package com.thread.exe;

import java.util.HashSet;
import java.util.Set;

public class Taxi {
	private Point location, destincation;
	private final Dispatcher dispatcher;
	
	public Taxi(Point location, Point destincation, Dispatcher dispatcher) {
		super();
		this.location = location;
		this.destincation = destincation;
		this.dispatcher = dispatcher;
	}
	
	public synchronized Point getLocation() {
		return location;
	}
	
	public void setLocation(Point location) {
		boolean reachedDestination;
		
		synchronized (this) {
			this.location = location;
			reachedDestination =  location.equals(destincation);
		}
		
		if(reachedDestination)
			dispatcher.notifyAvailable(this);
	}
}

class Dispatcher {
	private final Set<Taxi> availableTaxis = new HashSet<>();
	public synchronized void notifyAvailable(Taxi taxi) {
		availableTaxis.add(taxi);
	}
	
	public  void getImage() {
		Set<Taxi> copy;
		synchronized (this) {
			copy = new HashSet<>(availableTaxis);
		}
		for (Taxi taxi : copy) {
			taxi.getLocation();
		}
	}
}